layui.use(['form','layer'],function() {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;

    form.on('submit(*)', function(data){
        $.ajax({
            type: "POST",
            url: "/user/login",
            data: {
                username:$("#username").val(),
                password:md5($("#password").val())
            },
            dataType: "json",
            success: function(res){
                if(res.code == 0){
                    layer.msg(res.msg);
                    // 将用户信息存到浏览器缓存
                    localStorage.setItem("userId", res.data.id);
                    localStorage.setItem("nickname", res.data.nickname);
                    setTimeout(function () {
                        location.href = "/page/goods_list.htm";
                    }, 800);
                }else{
                    layer.msg(res.msg);
                }
            }
        });
        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    });

});